package com.choichumgming.rentcat.config;


import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;

import java.net.URL;
import java.nio.file.Paths;

/**
 * <p>
 *
 * </p>
 *
 * @author xiachaoyang
 * @version V1.0
 * @date 2018年08月21日 9:45
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2018年08月21日
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class Swagger2Markup {

    public static void generateAsciiDocs() throws Exception {
        //  输出Ascii格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ums/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/asciidoc/generated/ums"));
        Swagger2MarkupConverter.from(new URL("http://localhost:8888/pms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/asciidoc/generated/pms"));
        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/asciidoc/generated/oms"));
        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ams/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/asciidoc/generated/ams"));
        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oss/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/asciidoc/generated/oss"));
    }

    /**
     * 生成Markdown格式文档
     *
     * @throws Exception
     */
    public static void generateMarkdownDocs() throws Exception {
        //  输出Markdown格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.MARKDOWN)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ums/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/markdown/ums"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/pms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/markdown/pms"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ams/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/markdown/ams"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/markdown/oms"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oss/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/markdown/oss"));
    }


    /**
     * 生成Confluence格式文档
     *
     * @throws Exception
     */
    public void generateConfluenceDocs() throws Exception {
        //  输出Confluence使用的格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.CONFLUENCE_MARKUP)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ums/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("docs/confluence/generated"));
    }

    /**
     * 生成AsciiDocs格式文档,并汇总成一个文件
     *
     * @throws Exception
     */
    public void generateAsciiDocsToFile() throws Exception {
        //  输出Ascii到单文件
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ums/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFile(Paths.get("docs/asciidoc/generated/all"));
    }

    /**
     * 生成Markdown格式文档,并汇总成一个文件
     *
     * @throws Exception
     */
    public static void generateMarkdownDocsToFile() throws Exception {
        //  输出Markdown到单文件
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.MARKDOWN)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();


        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ums/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("../docs/markdown/generated/all/ums"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/pms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("../docs/markdown/generated/all/pms"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/ams/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("../docs/markdown/generated/all/ams"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oms/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("../docs/markdown/generated/all/oms"));

        Swagger2MarkupConverter.from(new URL("http://localhost:8888/oss/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("../docs/markdown/generated/all/oss"));
    }

    public static void main(String[] args) throws Exception {
        //createAsciidoc("acs-web/target/asciidoc/generated/","acs-web/target/swagger/",new String[]{"acs-ui-v1.json","acs-api-v1.json"});
        generateMarkdownDocs();
//        generateAsciiDocs();
    }
}


